package com.deer.wms.instock.service.enter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.base.system.model.Item.ItemInfo;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.instock.model.Allot.AllotMaster;
import com.deer.wms.instock.model.erpType.ErpType;
import com.deer.wms.instock.service.AllotMasterService;
import com.deer.wms.instock.service.enter.EnterErp;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import com.deer.wms.ware.task.service.SoDetailService;
import com.deer.wms.ware.task.service.SoMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.deer.wms.project.root.util.CollectionUtils.convertMap;

/**
 * @Author: xu
 * @Date: 2022/08/08/14:13
 * @Description:
 */
//41调拨入库
@Component
public class Allot implements EnterErp {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;
    @Autowired
    private SoDetailService soDetailService;
    @Autowired
    private ItemInfoService itemInfoService;
    @Autowired
    private AllotMasterService allotMasterService;

    @Override
    public ErpType gainType() {
       return new ErpType(null, 41);
    }

    @Override
    public Map<String, String> EnterBeanAndPush(AsnMaster asnMaster, List<AsnDetail> asnDetails, BaseAssist enterAssist) {
        String billNo = asnMaster.getBillNo();
        List<SoDetail> soDetails = soDetailService.list(new QueryWrapper<SoDetail>().eq("bill_no", billNo));
        Map<String, SoDetail> soMap = convertMap(soDetails, SoDetail::getDetailNo);
        OrderToErpBO orderToErpBO = new OrderToErpBO();
        orderToErpBO.setBillTypeId(41);
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("DB"));
        orderToErpBO.setBillDate(DateUtils.getNowDateString());
        AllotMaster allotMaster = allotMasterService.getOne(new QueryWrapper<AllotMaster>().eq("bill_no", billNo).last("limit 1"));
        orderToErpBO.setCustomerId(Optional.ofNullable(allotMaster).map(item -> item.getCustomerId()).orElse(null)); //客户id
        orderToErpBO.setCategoryId(ObjectUtil.isNotEmpty(enterAssist.getCategoryId()) ? enterAssist.getCategoryId() : 41146); //默认其他调拨
        List<OrderDetailBO> details = new ArrayList<>();
        Integer toErpAreaId = allotMaster.getToErpAreaId();
        asnDetails.forEach(item -> {
            SoDetail soDetail = soMap.get(item.getDetailNo());
            if (item.getQty()>soDetail.getPickQuantity()) {
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, soDetail.getItemCode() + "未拣货完毕");
            }

            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setAgreementNo(item.getMemo());
            orderDetailBO.setQty(item.getQty());//数量
            orderDetailBO.setBatchNo(item.getBatchName()); //计划跟踪号
            orderDetailBO.setStockId(asnMaster.getFromErpAreaId()); //调出仓库
            ItemInfo itemInfo = itemInfoService.getOne(new QueryWrapper<ItemInfo>().eq("item_code", item.getItemCode()));
            orderDetailBO.setItemId(itemInfo.getErpItemId());
            orderDetailBO.setStockId2(item.getStockId()); //调入仓库
            if (ObjectUtil.notEqual(item.getStockId(), toErpAreaId)) {
                throw new ServiceException(CommonCode.ORGANIZATION_CODE_EXISTS, "调入仓库不在不在目标仓库");
            }
            details.add(orderDetailBO);
        });
        orderToErpBO.setDetails(details);
        return  pushErpService.CreateIMBill(orderToErpBO);
    }
}
